<?php
require_once '../sub/init.php';
$currfields = 'sex,grade,openid,subscribe,money,mob,weixin,nickname,hh_num,weixin,photo_s,RZ,photo_f,myinfobfb,sign_pdf_flag';
require_once ZEAI.'m/my_chk_u.php';
require_once ZEAI.'cache/config_vip.php';
$t = (ifint($t,'1-3','1'))?$t:1;
if(ifint($id))$t=2;
$cook_grade = intval($row['grade']);
$cook_sex   = intval($row['sex']);
$cook_mob = trimhtml(dataIO($row['mob'],'out'));
$cook_weixin = trimhtml(dataIO($row['weixin'],'out'));
$cook_nickname = trimhtml(dataIO($row['nickname'],'out'));
if ($submitok == 'ajax_pass'){
	if (!ifint($uid))json_exit(array('flag'=>0,'msg'=>'forbidden_uid'));
	$db->query("UPDATE ".__TBL_HH__." SET ifagree=2 WHERE kind='$kind' AND uid=".$cook_uid." AND senduid=".$uid);
	//对方UID
	$rowta = $db->ROW(__TBL_USER__,"openid,subscribe","id=".$uid,"name");
	$ta_openid    = $rowta['openid'];
	$ta_subscribe = $rowta['subscribe'];
	//通知
	$title = $cook_nickname.' ID:'.$cook_uid.'，暂时不想与您互换'.$kind_str;
	$C     = '天下何处无芳草，有缘自会来相会';
	if(!empty($ta_openid) && $ta_subscribe==1){
		$first     = urlencode("您好");
		$keyword1  = urlencode($title);
		$keyword3  = urlencode($_ZEAI['siteName']);
		$remark    = urlencode($C);
		@wx_mb_sent('mbbh=ZEAI_ADMIN_INFO&openid='.$ta_openid.'&keyword1='.$keyword1.'&keyword3='.$keyword3.'&time='.ADDTIME.'&first='.$first.'&remark='.$remark.'&url='.$url);
	}
	//站内
	$db->SendTip($uid,$title,dataIO($title.'，'.$C,'in',1000),'sys');
	json_exit(array('flag'=>1,'msg'=>'拒绝成功'));
}elseif($submitok == 'ajax_hh_update'){
	if (!ifint($uid))json_exit(array('flag'=>0,'msg'=>'forbidden_uid'));
	//前置条件
	$cook_hh_num = intval($row['hh_num']);
	$cook_weixin = trimhtml(dataIO($row['weixin'],'out'));
	$cook_photo_s = $row['photo_s'];
	$cook_photo_f = intval($row['photo_f']);
	$cook_RZ = $row['RZ'];$cook_RZarr = explode(',',$cook_RZ);
	$cook_myinfobfb = intval($row['myinfobfb']);
	$cook_sign_pdf_flag  = $row['sign_pdf_flag'];
	//
	$hh_data = explode(',',$_VIP['hh_data']);
	if(count($hh_data)>0 && is_array($hh_data)){
		if(empty($cook_photo_s))$cook_photo_f=0;
		$row0 = $db->ROW(__TBL_USER__,"sex","id=".$uid,"num");$sexx=$row0[0];
		$json=array("ARR"=>$hh_data,"jumpurl"=>$jumpurl,"RZarr"=>$cook_RZarr,"sex"=>$sexx,"cook_sex"=>$cook_sex,"cook_grade"=>$cook_grade,"cook_photo_f"=>$cook_photo_f,"cook_myinfobfb"=>$cook_myinfobfb,"data_bfb_num"=>intval($_VIP['hh_bfb_num']),"cook_sign_pdf_flag"=>$cook_sign_pdf_flag,"js"=>$js);
		Before_TJ(encode_json($json));
	}
	//
	$content=trimhtml($content);
	if($kind=='mob'){
		$kind_str='手机号码';
		if(!ifmob($content))json_exit(array('flag'=>0,'msg'=>'请输入您要互换的【'.$kind_str.'】'));
	}else{
		$kind=='weixin';$kind_str='微信号';
		if(str_len($content)<4)json_exit(array('flag'=>0,'msg'=>'请输入您要互换的【'.$kind_str.'】'));
	}
	$row = $db->ROW(__TBL_HH__,"id"," kind='$kind' AND uid=".$cook_uid." AND senduid=".$uid."   ","name");
	if ($row){
		$hhid=$row['id'];
		$db->query("UPDATE ".__TBL_HH__." SET uid_content='$content',uid_ifagree_time=".ADDTIME.",ifagree=1 WHERE id=".$hhid);
	}else{
		json_exit(array('flag'=>0,'msg'=>'互换记录跑路了'));
	}
	//主表空，更新
	if(empty($cook_weixin) && $kind=='weixin'){
		$weixin = dataIO($content,'in',50);
		$db->query("UPDATE ".__TBL_USER__." SET weixin='$weixin' WHERE id=".$cook_uid);
		$row_shalou=shalou_data($cook_uid);
		$row_shalou['weixin'] = $weixin;
		$jsonstr = encode_json($row_shalou);
		$db->query("UPDATE ".__TBL_SHALOU__." SET jsonstr='$jsonstr' WHERE uid=".$cook_uid);
	}
	//对方UID
	$rowta = $db->ROW(__TBL_USER__,"openid,subscribe","id=".$uid,"name");
	$ta_openid    = $rowta['openid'];
	$ta_subscribe = $rowta['subscribe'];
	//通知
	$title = $cook_nickname.' UID:'.$cook_uid."已同意您的".$kind_str."互换请求！";
	$C     = 'Ta的'.$kind_str.'为：'.$content;
	if(!empty($ta_openid) && $ta_subscribe==1){
		$first     = urlencode("恭喜您，".$title);
		$keyword1  = urlencode('同意互换');
		$keyword3  = urlencode($_ZEAI['siteName']);
		$remark    = urlencode('Ta的'.$kind_str.'为：'.$content);
		$url       = urlencode(HOST.'/m/my_huhuan.php');
		@wx_mb_sent('mbbh=ZEAI_ADMIN_INFO&openid='.$ta_openid.'&keyword1='.$keyword1.'&keyword3='.$keyword3.'&time='.ADDTIME.'&first='.$first.'&remark='.$remark.'&url='.$url);
	}
	//站内
	$db->SendTip($uid,$title,dataIO($title.$C,'in',1000),'sys');
	json_exit(array('flag'=>1,'msg'=>'操作成功'));
}
//
$t_str='互换联系方法';
$nodatatips="<div class='nodatatips'><i class='ico'>&#xe608;</i>暂时木有</div>";
function rows_ulist($rows,$p) {
	global $_ZEAI,$t;
	$id      = $rows['id'];
	$uid     = $rows['uid'];
	$kind     = $rows['kind'];
	$nickname = trimhtml(dataIO($rows['nickname'],'out'));
	$content = trimhtml(dataIO($rows['content'],'out'));
	$sex      = $rows['sex'];
	$grade    = $rows['grade'];
	$photo_s  = $rows['photo_s'];
	$photo_f  = $rows['photo_f'];
	$areatitle= $rows['areatitle'];
	$birthday = $rows['birthday'];
	$heigh    = $rows['heigh'];
	$ifagree  = $rows['ifagree'];
	$flag     = $rows['flag'];
	$photo_ifshow = $rows['photo_ifshow'];
	$birthday_str  = (getage($birthday)<=0)?'':getage($birthday).'岁<b>・</b>';
	$heigh_str     = (empty($heigh))?'':$heigh.'cm';
	$aARR = explode(' ',$areatitle);$areatitle = $aARR[1].$aARR[2];
	//$areatitle_str = (empty($areatitle))?'':$areatitle;
	$photo_s_url = photo_s_out('{"smb":"s","sex":"'.$sex.'","photo_s":"'.$photo_s.'","photo_f":"'.$photo_f.'","photo_ifshow":"'.$photo_ifshow.'","uflag":"'.$flag.'","outkind":"url"}');	
	if($flag==-2)$nickname='***';
	$tmpid=$id;
	if($kind=='mob'){
		$kind='mob';$kind_str='手机号码';$ico='<i class="ico">&#xe627;</i>';
	}else{
		$kind='weixin';$kind_str='微信号';$ico='<i class="ico">&#xe607;</i>';
	}
	$uinfo=$birthday_str.$heigh_str.$areatitle_str;
	if($ifagree==1){
		$buttonstr= '<button class="success" type="button" >'.$ico.'互换成功';$btncls=' class="ed"</button>';
		$uinfo= '<a class="btn size1 BAI yuan" onclick="zeai.copy(\''.$content.'\',function(){zeai.msg(\'复制成功\');})">复制'.$kind_str.'：'.$content.'</a>';
	}elseif($ifagree==2){
		$buttonstr='<button class="pass" type="button">'.$ico.'已拒绝</button>';
	}else{
		switch ($t) {
			case 1:
				$buttonstr='<button class="wait" type="button">'.$ico.'等待对方同意</button>';
			break;
			case 2:
				$buttonstr = '<button class="agree_btn" type="button" onClick="hh_btnFn(1,\''.$kind.'\','.$uid.')">同意</button>';
				$buttonstr.= '<button class="pass_btn" type="button" onClick="hh_btnFn(0,\''.$kind.'\','.$uid.')">拒绝</button>';
				$buttonstr.= '<div class="kind_str">想与你互换'.$kind_str.'</div>';
			break;
			case 3:
				
			break;
		}
	}
	$O  = '<dl>';
	$O .= '<dt><a href="'.a('{"kind":"u","uid":"'.$uid.'"}').'"><img src="'.$photo_s_url.'"></a></dt>';
	$O .= '<dd><h4>'.uicon($sex.$grade).$nickname.'</h4><h6>'.$uinfo.'</h6></dd>';
	$O .= $buttonstr;
	$O .= '</dl>';
	return $O;
}
$_ZEAI['pagesize'] = 15;
switch ($t) {
	case 1:
		$ZEAI_SQL = "a.senduid=".$cook_uid." AND a.uid=b.id";
		$ZEAI_SELECT="SELECT a.id,a.uid,a.kind,a.ifagree,a.uid_content AS content,b.nickname,b.sex,b.grade,b.photo_s,b.photo_f,b.areatitle,b.birthday,b.heigh,b.flag,b.photo_ifshow FROM ".__TBL_HH__." a,".__TBL_USER__." b WHERE ".$ZEAI_SQL." ORDER BY a.addtime DESC";
	break;
	case 2:
		$ZEAI_SQL = "a.uid=".$cook_uid." AND a.senduid=b.id";
		$ZEAI_SELECT="SELECT a.id,a.senduid AS uid,a.kind,a.ifagree,a.senduid_content AS content,b.nickname,b.sex,b.grade,b.photo_s,b.photo_f,b.areatitle,b.birthday,b.heigh,b.flag,b.photo_ifshow FROM ".__TBL_HH__." a,".__TBL_USER__." b WHERE ".$ZEAI_SQL." ORDER BY a.ifagree,a.addtime DESC";
	break;
	case 3:
		$ZEAI_SQL = "a.senduid=".$cook_uid." AND a.uid=b.id AND a.ifagree=1";
		$ZEAI_SELECT="SELECT a.id,a.uid,a.kind,a.ifagree,a.uid_content AS content,b.nickname,b.sex,b.grade,b.photo_s,b.photo_f,b.areatitle,b.birthday,b.heigh,b.flag,b.photo_ifshow FROM ".__TBL_HH__." a,".__TBL_USER__." b WHERE ".$ZEAI_SQL." ORDER BY a.addtime DESC";
	break;
}
if($submitok=='ZEAI_list'){exit(Zeai_ajax_list_fn($ZEAI_totalP,$p));}
$rtt=$db->query("SELECT COUNT(*) FROM ".__TBL_HH__." a,".__TBL_USER__." b WHERE ".$ZEAI_SQL);
$roww = $db->fetch_array($rtt,'num');
$ZEAI_total = $roww[0];
$ZEAI_totalP= ceil($ZEAI_total/$_ZEAI['pagesize']);
$nav='my';?>
<!doctype html><html><head><meta charset="utf-8">
<title>我的-<?php echo $_ZEAI['siteName'];?></title>
<?php echo HEADMETA; ?>
<link href="<?php echo RES;?>m/css/m.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
<link href="<?php echo HOST;?>/cache/zeaicolor.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
<link href="<?php echo RES;?>m/css/my_huhuan.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
<script src="<?php echo RES;?>www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo RES;?>m/js/m.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo RES;?>m/js/my_huhuan.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script>var t=<?php echo $t;?></script>
<style>
.tabmenu a.ed:after{background-color:<?php echo $_ZEAI['mob_mbcolor'];?>}
header.bgcolor{background-color:<?php echo $_ZEAI['mob_mbcolor'];?>}
#backtop a,.btmKefuBtn{background-color:<?php echo $_ZEAI['mob_mbcolor'];?>}
.my_hn_yes .admid .hnpic b{background-color:rgba(<?php echo c16_rgb($_ZEAI['mob_mbcolor']);?>,.8)}
.my_hn_yes .admid em.hnpic{border-color:<?php echo $_ZEAI['mob_mbcolor'];?>}
<?php
if ($_ZEAI['m_mb']==2){
	echo '.tabmenufix{width:70%;max-width:500px;top:1.5rem;margin:auto;left:0;right:0}';
	echo '.my_huhuan{margin-top:6rem}';
	$mini_class='top_mini4';
}else{
	echo '.tabmenufix{top:6.7rem}';
	$mini_title=$t_str;
	$mini_class='top_mini4 zeaibg bgcolor yunbox';
}
?>
</style>
</head>
<body>
<?php
require_once ZEAI.'m/top_mini.php';
$rtt2=$db->query("SELECT COUNT(*) FROM ".__TBL_HH__." a,".__TBL_USER__." b WHERE a.uid=".$cook_uid." AND a.senduid=b.id AND a.ifagree=0");
$roww2 = $db->fetch_array($rtt2,'num');
$total2=$roww2[0];
$total2 = ($total2>0)?'<b>'.$total2.'</b>':'';
?>
<div class="tabmenu tabmenu3 tabmenufix">
	<a href="my_huhuan.php?t=1"<?php echo ($t == 1)?' class="ed"':'';?>><span>我发起的</span></a>
	<a href="my_huhuan.php?t=2"<?php echo ($t == 2)?' class="ed"':'';?>><span>收到请求</span><?php echo $total2;?></a>
	<a href="my_huhuan.php?t=3"<?php echo ($t == 3)?' class="ed"':'';?>><span>互换成功</span></a>
</div>
<form id="hh_weixin_form" class="hhbox_form">
    <div><i class="ico wxi">&#xe607;</i><input class="input" name="content" id="content" placeholder="请输入本人微信号" maxlength="30" value="<?php echo $cook_weixin;?>"></div>
    <em>确保您本人【微信号】必须真实有效，否则将纳入失信人员名单并进行<font class="Cf00">封号处理</font></em>
    <input type="hidden" name="uid" id="uid_weixin">
    <input type="hidden" name="submitok" value="ajax_hh_update">
	<input type="hidden" name="kind" value="weixin">
    <button type="button" class="btn size3 zeaibg" onClick="hhsend('hh_weixin_form')">同意并发送</button>
</form>
<form id="hh_mob_form" class="hhbox_form">
    <div><i class="ico mobi">&#xe627;</i><input class="input" name="content" id="content" placeholder="请输入本人手机号码" maxlength="11" value="<?php echo $cook_mob;?>"></div>
    <em>确保您本人【手机号码】必须真实有效，否则将纳入失信人员名单并进行<font class="Cf00">封号处理</font></em>
    <input type="hidden" name="uid" id="uid_mob">
    <input type="hidden" name="submitok" value="ajax_hh_update">
    <input type="hidden" name="kind" value="mob">
    <button type="button" class="btn size3 zeaibg" onClick="hhsend('hh_mob_form')">同意并发送</button>
</form>
<?php
if (ifint($id)){
	$row = $db->ROW(__TBL_HH__,"kind,senduid,ifagree,senduid_content"," uid=".$cook_uid." AND id=".$id,"name");
	if ($row){
		$kind=$row['kind'];
		$senduid=$row['senduid'];
		$ifagree=$row['ifagree'];
		$kind = $row['kind'];
		$senduid_content = trimhtml(dataIO($row['senduid_content'],'out'));
	}else{
		zeaialert('{"title":"互换记录跑路了","btnt":"我知道了","url":"-1"}');
	}
	$rowta = $db->ROW(__TBL_USER__,"nickname,sex,grade,photo_s,photo_f,areatitle,birthday,heigh,flag,photo_ifshow","id=".$senduid,"name");
	$nickname = trimhtml(dataIO($rowta['nickname'],'out'));
	$sex      = $rowta['sex'];
	$grade    = $rowta['grade'];
	$photo_s  = $rowta['photo_s'];
	$photo_f  = $rowta['photo_f'];
	$areatitle= $rowta['areatitle'];
	$birthday = $rowta['birthday'];
	$heigh    = $rowta['heigh'];
	$flag     = $rowta['flag'];
	$photo_ifshow = $rowta['photo_ifshow'];
	$birthday_str  = (getage($birthday)<=0)?'':getage($birthday).'岁<b>·</b>';
	$heigh_str     = (empty($heigh))?'':$heigh.'cm<b>·</b>';
	$aARR = explode(' ',$areatitle);$areatitle = $aARR[1].$aARR[2];
	$areatitle_str = (empty($areatitle))?'':$areatitle;
	$photo_s_url = (!empty($photo_s) && $photo_f==1)?UP.$photo_s:RES.'photo_m'.$sex.'.png';
	if($photo_ifshow==0 || $flag==-2 || ifmLockBlur()){$photo_s_url=RES.'photo_m'.$sex.'_hide.png';}
	if($flag==-2)$nickname='***';
	if($kind=='mob'){
		$kind='mob';$kind_str='手机号码';$ico='<i class="ico mob">&#xe627;</i>';
	}else{
		$kind='weixin';$kind_str='微信号';$ico='<i class="ico weixin">&#xe607;</i>';
	}
	$uinfo=$birthday_str.$heigh_str.$areatitle_str;
	?>
    <div class="my_hn_yes">
        <div class="admid"><em class="hnpic"><a href="<?php echo mHref('u',$senduid);?>"><img src="<?php echo $photo_s_url;?>" /></a><b>互换联系</b></em></div>
        <br><?php echo uicon($sex.$grade);?><?php echo $nickname;?> (UID：<?php echo $senduid;?>)
        <?php echo '<div class="S14 C999" style="margin-top:10px">'.$uinfo.'</div>';?>
        <?php if ($ifagree==1){?>
        	<div class="success"><?php echo $ico.$kind_str;?>互换成功
            <span>Ta的<?php echo $kind_str;?>为：<?php echo $senduid_content;?></span>
            <a class="btn size1 BAI yuan" onclick="zeai.copy('<?php echo $senduid_content;?>',function(){zeai.msg('复制成功');})">复制<?php echo $kind_str;?></a>
            </div>
        <?php }elseif($ifagree==2){ ?>
        	<div class="pass"><?php echo $ico.$kind_str;?>请求已拒绝</div>
        <?php }else{ ?>
            <div class="btnbox">
            <button class="agree_btn" type="button" onClick="hh_btnFn(1,'<?php echo $kind;?>',<?php echo $senduid;?>)">同意</button>
            <button class="pass_btn" type="button" onClick="hh_btnFn(0,'<?php echo $kind;?>',<?php echo $senduid;?>)">拒绝</button>
            <div class="kind_str">想与你互换<?php echo $kind_str.$ico;?></div>
            </div>
        <?php }?>
    </div>
<?php }else{ ?>
    <div class="my_huhuan" id="ZEAI_list"><?php if($ZEAI_totalP>0){echo Zeai_ajax_list_fn($ZEAI_totalP,$p);}else{echo $nodatatips;}?></div>
    <?php if ($ZEAI_total > $_ZEAI['pagesize']){?>
    <script>
    var ZEAI_totalP = parseInt(<?php echo $ZEAI_totalP; ?>),p=2;
    zeaiOnscroll_json={url:'my_huhuan'+zeai.extname,data:{submitok:'ZEAI_list',ZEAI_totalP:ZEAI_totalP,t:<?php echo $t;?>}};
    document.body.onscroll = zeaiOnscroll;
    </script>
    <div id="backtop"><a href="#top" id="btmTopBtn"><i class="ico">&#xe60c;</i>顶部</a></div>
    <?php }?>
<?php }
if($_ZEAI['m_mb']==2){
	require_once ZEAI.'m/bottom2.php';
}else{
	require_once ZEAI.'m/TRnav.php';
	require_once ZEAI.'m/bottom.php';
}
?>